package com.kele.goods.controller;

import com.kele.api.goods.SpecificationControllerApi;
import com.kele.common.model.response.PageResult;
import com.kele.common.model.response.Result;
import com.kele.goods.service.SpecificationService;
import com.kele.model.goods.Specification;
import com.kele.model.goods.dto.SpecificationDTO;
import com.kele.model.goods.dto.SpecificationDTO1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("specification")
public class SpecificationController implements SpecificationControllerApi {

    @Autowired
    SpecificationService specificationService;

    @Override
    @GetMapping("findAll")
    public List<Specification> findAll() {
        return specificationService.findAll();
    }

    @Override
    @PostMapping("findPage/{page}/{size}")
    public PageResult<Specification> findPage(@PathVariable int page,
                                              @PathVariable int size,
                                              @RequestBody Map searchMap) {
        return specificationService.findPage(page,size,searchMap);
    }

    @Override
    @GetMapping("findById/{id}")
    public SpecificationDTO findById(@PathVariable Long id) {
        return specificationService.findById(id);
    }

    @Override
    @PostMapping("save")
    public Result save(@RequestBody SpecificationDTO specificationDTO) {
        Long id = specificationDTO.getId();
        if(id == null){
            return specificationService.add(specificationDTO);
        }else{
            return specificationService.update(specificationDTO);
        }
    }

    @Override
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Long id) {
        return specificationService.delete(id);
    }

    @Override
    @GetMapping("deleteByIds/{ids}")
    public Result deleteByIds(Long[] ids) {
        return specificationService.deleteByIds(ids);
    }


    @PostMapping("findPage1/{page}/{size}")
    public PageResult<SpecificationDTO1> findPage1(@PathVariable int page,
                                                   @PathVariable int size,
                                                   @RequestBody Map searchMap) {
        return specificationService.findPage1(page,size,searchMap);
    }

    @PostMapping("findPage2/{page}/{size}")
    public PageResult<SpecificationDTO1> findPage2(@PathVariable int page,
                                                   @PathVariable int size,
                                                   @RequestBody Map searchMap) {
        return specificationService.findPage2(page,size,searchMap);
    }
}
